package com.nuc.kl.module.usermanager.action;


import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nuc.kl.base.BaseAction;
import com.nuc.kl.module.basedata.entity.Direction;
import com.nuc.kl.module.usermanager.entity.Role;
import com.nuc.kl.module.usermanager.entity.Student;
import com.nuc.kl.module.usermanager.entity.Teacher;
import com.nuc.kl.module.usermanager.entity.User;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class StudentAction extends BaseAction<Student>{
	
	
	private String ids;// 删除所用多个id
	private String findName;// 模糊查询所用
	private JSONObject result;// 返回的json
	private String rows;// 每页显示的记录数
	private String page;// 当前第几页
	
	private String directionId;
	
	/** 列表 */
	public String listJson() throws Exception {
		// 当前页
		int intPage = Integer.parseInt((page == null || page == "0") ? "1"
				: page);
		// 每页显示条数
		int number = Integer.parseInt((rows == null || rows == "0") ? "10"
				: rows);
		// 每页的开始记录 第一页为1 第二页为number +1
		int start = (intPage - 1) * number;
		
		List<Student> list = null;
		
	
		list = studentService.findAllPage(start, number);
		
		String[] s= {};
		toBeJson(list, studentService.count(model),s );
		
		return null;
	}

	
	
	/** 列表 */
	public String list() throws Exception {
		
		return "list";
	}

	/** 删除 */
	public String delete() throws Exception {
		studentService.delete(model.getId());
		
		return "toList";
	}
	/** 同时删除多个 */
	public String deleteByIds() throws Exception {
		String[] idList = ids.split(",");
		for (String str : idList) {
			studentService.delete(Long.parseLong(str));
		}
		return "toList";
	}
	
	/** 添加页面 */
	public String addUI() throws Exception {
		
		return null;
	}

	/** 添加 */
	public String add() throws Exception {
		String md5Digest = DigestUtils.md5Hex("123123");
		Direction direction = directionService.getById(Long.parseLong(directionId));
		
		model.setPassword(md5Digest);
		model.setDirection(direction);
		studentService.save(model);
		return "toList";
	}

	/** 修改页面 */
	public String editUI() throws Exception {
		
		
		return "saveUI";
	}

	/** 修改 */
	public String edit() throws Exception {
		Student student = studentService.getById(model.getId());
		student.setLoginName(model.getLoginName());
		student.setPassword(model.getPassword());
		student.setName(model.getName());
		student.setStuNum(model.getStuNum());
		student.setDirection(directionService.getById(Long.parseLong(directionId)));
		student.setEntryYear(model.getEntryYear());
		studentService.update(student);
		return "toList";
	}
	
	public String initPassword() throws Exception {
		Student student=studentService.getById(model.getId());
		String md5Digest = DigestUtils.md5Hex("123123");
		student.setPassword(md5Digest);
		studentService.update(student);
		return null;
	}
	
	public String getAllDirection() throws Exception{
		List<Direction> directions = directionService.findAllDirection();
		String[] str ={};
		listToJson(directions, str);
		return null;
	}
	
	//--------------------------


	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getFindName() {
		return findName;
	}

	public void setFindName(String findName) {
		this.findName = findName;
	}

	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}



	public String getDirectionId() {
		return directionId;
	}



	public void setDirectionId(String directionId) {
		this.directionId = directionId;
	}
	
	
}
